<?php

$view['name'] = "view_contacts";
$view['version'] = "20250920";
$view['description'] = "Show the contacts with assigned addresses, phones, emails, urls, users, groups, settings, and relations";
$view['sql'] = "SELECT \n";
$view['sql'] .= "c.*, \n";
$view['sql'] .= "( \n";
$view['sql'] .= "	SELECT json_agg(a.*) \n";
$view['sql'] .= "	FROM v_contact_addresses as a \n";
$view['sql'] .= "	WHERE a.contact_uuid = c.contact_uuid \n";
$view['sql'] .= ") AS contact_addresses, \n";
$view['sql'] .= "( \n";
$view['sql'] .= "	SELECT json_agg(p.*) \n";
$view['sql'] .= "	FROM v_contact_phones as p \n";
$view['sql'] .= "	WHERE p.contact_uuid = c.contact_uuid \n";
$view['sql'] .= ") AS contact_phones, \n";
$view['sql'] .= "( \n";
$view['sql'] .= "	SELECT json_agg(e.*) \n";
$view['sql'] .= "	FROM v_contact_emails as e \n";
$view['sql'] .= "	WHERE e.contact_uuid = c.contact_uuid \n";
$view['sql'] .= ") AS contact_emails, \n";
$view['sql'] .= "( \n";
$view['sql'] .= "	SELECT json_agg(l.*) \n";
$view['sql'] .= "	FROM v_contact_urls as l \n";
$view['sql'] .= "	WHERE l.contact_uuid = c.contact_uuid \n";
$view['sql'] .= ") AS contact_urls, \n";
$view['sql'] .= "( \n";
$view['sql'] .= "	SELECT json_agg(u.*) \n";
$view['sql'] .= "	FROM v_contact_users as u \n";
$view['sql'] .= "	WHERE u.contact_uuid = c.contact_uuid \n";
$view['sql'] .= ") AS contact_users, \n";
$view['sql'] .= "( \n";
$view['sql'] .= "	SELECT json_agg(g.*) \n";
$view['sql'] .= "	FROM v_contact_groups as g \n";
$view['sql'] .= "	WHERE g.contact_uuid = c.contact_uuid \n";
$view['sql'] .= ") AS contact_groups, \n";
$view['sql'] .= "( \n";
$view['sql'] .= "	SELECT json_agg(s.*) \n";
$view['sql'] .= "	FROM v_contact_settings as s \n";
$view['sql'] .= "	WHERE s.contact_uuid = c.contact_uuid \n";
$view['sql'] .= ") AS contact_settings, \n";
$view['sql'] .= "( \n";
$view['sql'] .= "	SELECT json_agg(r.*) \n";
$view['sql'] .= "	FROM v_contact_relations as r \n";
$view['sql'] .= "	WHERE r.contact_uuid = c.contact_uuid \n";
$view['sql'] .= ") AS contact_relations \n";
$view['sql'] .= "FROM \n";
$view['sql'] .= "v_contacts as c \n";
$view['sql'] .= "); \n";
